package com.chance.cc.crawler.prod.command.job.domain.commerce.dangdang.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.commerce.vip.VipCommonCrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @ClassName dangdang
 * @Description TODO
 * @Author songding
 * @Date 2021/10/19 16:40
 * @Version 1.0
 **/
public class DangdangArticleCrawlerScheduleJob extends VipCommonCrawlerScheduleJob {
    public static void main(String[] args) {
        publishVipCommentSchedulerJob();
    }

    public static CrawlerJob publishVipCommentSchedulerJob() {
        CrawlerJob dangdangCrawlerJob = dangdangCrawlerJob();
        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(dangdangCrawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("当当网每天采集最新文章");
        crawlerScheduleJob.setCrawlerKey(dangdangCrawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return dangdangCrawlerJob;
    }


    public static CrawlerJob dangdangCrawlerJob() {
        String site = "article";
        String site_biz = "article-realtime";

        String url = "https://search.dangdang.com/";
        CrawlerRequestRecord record = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .httpConfig(HttpConfig.me(domain))
                .httpUrl(url)
                .recordKey(url)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1, null))
                .needParsed(true)
                .needWashed(false)
                .build();
        record.tagsCreator().bizTags().addDomain(domain);
        record.tagsCreator().bizTags().addSite(site);
        record.tagsCreator().bizTags().addSiteBiz(site_biz);

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dangdang_search", turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/dangdang/keys?site=searchKw")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(5)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),//vip-article-article-realtime-realtime
                        StringUtils.joinWith("-", site, site_biz, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))//crawler-dcdapp-forum-forum-realtime-queue
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, site, site_biz, "queue")))
                .kafkaResultPipeline(null, kafkaTopic, null)
                .fileResultPipeline(null, "/data/chance_crawler_runner/logs/node/dangdang/dangdangArticle.log", false)
                .supportRecord(keywordRecord)
                .requestRecord(record)
                .build();
    }


}